package com.hnucm.springjdbc.dao.impl;
import com.hnucm.springjdbc.dao.BookDao;
import com.hnucm.springjdbc.pojo.Books;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class BookDaoImpl implements BookDao {
    @Autowired
    JdbcTemplate jdbcTemplate;
    public void addBooks(String name,double price, int amount) {
        jdbcTemplate.update("insert into bookinfo values(null,?,?,?)",name,price,amount);
    }

    public List<Books> findAllBooks() {
        RowMapper<Books> rowMapper=new BeanPropertyRowMapper<Books>(Books.class);
        return   jdbcTemplate.query("select * from bookinfo",rowMapper);
    }

    public void deleteBooks(int id) {
        jdbcTemplate.update("delete from bookinfo where id=?",id);
    }

    public void updataBooks(int id, String name,double price, int amount) {
        jdbcTemplate.update("update bookinfo set name=?,price=?,amount=? where id=?",name,price,amount,id);
    }
}
